home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: gail.ripco.com!mambuhl
- From: mambuhl@ripco.com (Martin Ambuhl)
- Subject: Re: Please help ?!
- X-Nntp-Posting-Host: foley.ripco.com
- Message-ID: <DLHv58.Cn1@rci.ripco.com>
- Sender: usenet@rci.ripco.com (Net News Admin)
- Organization: Ripco Internet BBS Chicago
- Date: Sat, 20 Jan 1996 19:22:19 GMT
- X-Ident-Sender: mambuhl
-
- mv@pi.net
- in <4dm889$3hs@neptunus.pi.net> asks:
-
- >Hello everybody,
-
-
- >Please take a look at this:
-
- [snip]
-
- >/*
-
- > Here it should print:
-
- >Switch: /w:test
- >Switch: /1
- >etc..
-
- >but it prints:
-
- >Switch: /w:test
- >Switch:
- >Switch: /1
- >and so on....
- >It jjust skips one array index everytime... It seems to go wrong in get_switche
- >()...
- >*/
-
- Martijn,
- Your code is logically a mess, but should also have never compiled.
- I have not changed the logic of your code, but have cleaned it up
- enough so that it will compile and run. Turn your diagnostics back
- on. The compiler issues them for a reason. The cleaned up code is
- below, with my changes marked with "/* mha ... */". You still need
- to clean up the logic, even though it works as is. I don't see how
- your code ever did anything as you posted it.
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #if 0
- #include <conio.h> /* mha - non-standard header serving no
- * real purpose in this code */
- #include <alloc.h> /* mha - non-standard header which
- * _never_ serves any purpose */
- #endif
-
- #define MAXLEN 20
- #define MAXSWITCH 20
-
- char *s = "dir /w:test/1/2/3/4";
- char *s2, *s3;
- char myswitches[MAXSWITCH][MAXLEN];
-
- int get_switches(char *cmd, char switches[][MAXLEN]
- /* mha - was "*switches[][MAXLEN]" */ )
- {
- int i = 0;
- char *cpy;
- char *saved;
-
- if (strlen(cmd) == 0) {
- return 0;
- }
- cpy = malloc(MAXLEN); /* mha - removed possibly error-masking
- * and certainly useless cast (char *) */
- if (cpy == NULL) {
- printf("Out of memory in %s on line: %d\n", __FILE__, __LINE__);
- exit(EXIT_FAILURE);
- }
- *cpy = '\0'; /* mha - replaced illegal and wasteful
- * "strcpy(cpy, '\0');" */
- saved = cpy;
- while (*cmd != '\0') {
- if (*cmd == '/') {
- if (i == MAXSWITCH) {
- printf("INT: Too many switches ...\n");
- return 9999;
- }
- *cpy = *cmd;
- do {
- *cpy++ = *cmd++;
- } while (!strchr(" /-\0", *cmd));
- *cpy = '\0';
- cpy = saved;
- strcpy(switches[i], cpy);
- *cpy = '\0'; /* mha - replaced illegal and wasteful
- * "strcpy(cpy, '\0');" */
- i++;
- }
- if (*cmd == '-') {
- if (i == MAXSWITCH) {
- printf("INT: Too many switches ...\n");
- return 9999;
- }
- *cpy = *cmd;
- do {
- *cpy++ = *cmd++;
- } while (!strchr(" /-\0", *cmd));
- *cpy = '\0';
- cpy = saved;
- strcpy(switches[i], cpy);
- *cpy = '\0'; /* mha - replaced illegal and wasteful
- * "strcpy(cpy, '\0');" */
- i++;
- }
- if (!strchr("/-\0", *cmd)) {
- *cmd++; /* mha - what is this supposed to be?
- * I'm sure this does _not_ do whatever you
- * intended! (No change made, since the intent
- * is no clearer to me than to your compiler) */
- }
- }
- free(cpy);
- return i;
- }
-
-
- int main(void)
- {
- int i;
- #if 0
- clrscr(); /* mha - removed this pointless
- * non-standard function call */
- #endif
- i = get_switches(s, myswitches);
- printf("Switches found: %i\n", i);
- for (i = 0; i < MAXSWITCH; i++) {
- printf("Switch: %s\n", myswitches[i]);
- }
- return 0;
- }
-
-
- --
- * Martin Ambuhl net: mambuhl@ripco.com
- * Chicago, IL (USA)
-